//*********************************************************
//
//    Copyright (c) Microsoft. All rights reserved.
//    This code is licensed under the Apache License, Version 2.0.
//    THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
//    ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
//    IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
//    PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
//
//********************************************************* 

using Microsoft.TridentWordAddIn.Common;

namespace Microsoft.TridentWordAddIn.Presentation
{
    internal class ViewConnectionsCommand : RibbonCommand
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="ViewConnectionsCommand"/> class.
        /// </summary>
        /// <param name="controller">The controller.</param>
        public ViewConnectionsCommand(IController controller)
            : base(controller)
        {
        }

        /// <summary>
        /// Gets a value indicating whether the originating control needs to be invalidated.
        /// </summary>
        /// <value>
        /// 	<c>true</c> if the control needs to be invalidated; otherwise, <c>false</c>.
        /// </value>
        public override bool NeedsInvalidate
        {
            get
            {
                return true; // since the toggle action can get canceled, we need to requery always.
            }
        }

        /// <summary>
        /// Invokes the command assuming toggle button is pressed.
        /// </summary>
        /// <param name="controlId">The originating control id.</param>
        public override void Invoke(string controlId)
        {
            this.Invoke(true);
        }

        /// <summary>
        /// Invokes the command
        /// </summary>
        /// <param name="isPressed">if set to <c>true</c> is toggle button is pressed.</param>
        public override void Invoke(bool isPressed)
        {
            if (!ControllerInstance.CurrentConnectionName.IsNullOrEmpty())
            {
                TridentConnection connection = ControllerInstance.UserData.Connections[ControllerInstance.CurrentConnectionName];
                TridentConnection connectionReturn = null;
                if (ControllerInstance.TestConnectionAndPromptIfRequired(connection, ref connectionReturn, true))
                {
                    base.ShowTaskPane(TaskPaneKey.Connections, isPressed);
                }
                else
                {
                    this.ControllerInstance.RemoveConnection(connection.Name);
                    this.ControllerInstance.ConnectionChanged(false, ConnectionOperation.Deleted);
                    this.ControllerInstance.SetNextDefaultConnection();
                    base.ShowTaskPane(TaskPaneKey.Connections, isPressed);
                }
            }
            else
            {
                base.ShowTaskPane(TaskPaneKey.Connections, isPressed);
            }            
        }
    }
}
